package com.hershel.userservice.controller;

import com.hershel.hershelcommonpackage.entity.Result;
import com.hershel.hershelcommonpackage.exception.DataBaseQueryNullException;
import com.hershel.userservice.constant.ErrorConstant;
import com.hershel.userservice.entity.UserDetails;
import com.hershel.userservice.service.UserService;
import com.hershel.userservice.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录并生成token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(HttpServletRequest request, HttpServletResponse response, @RequestBody UserDetails user) {
        Result result = new Result();
        try {
            if (StringUtils.isEmpty(user.getUsername())) {
                LOGGER.error(ErrorConstant.USERNAME_IS_NULL);
                response.setStatus(500);
                return Result.error(ErrorConstant.USERNAME_IS_NULL);
            } else if (StringUtils.isEmpty(user.getPassword())) {
                LOGGER.error(ErrorConstant.PASSWORD_IS_NULL);
                response.setStatus(500);
                return Result.error(ErrorConstant.PASSWORD_IS_NULL);
            }
            boolean login = userService.login(user.getUsername(), user.getPassword());
            if (!login) {
                LOGGER.error(ErrorConstant.PASSWORD_ERROR);
                response.setStatus(500);
                return Result.error(ErrorConstant.PASSWORD_ERROR);
            }
            String token = JwtUtil.generateToken(user.getUsername());
            LOGGER.info("token:{}", token);
            Map map = new HashMap();
            map.put("token", token);
            return result.okHaveData(map);
        } catch (DataBaseQueryNullException e) {
            response.setStatus(500);
            return result.error("该账号不存在，请进行注册。");
        }
    }

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(HttpServletRequest request, HttpServletResponse response, @RequestBody UserDetails user) {
        Result result = new Result();
        try {
            LOGGER.info("user:{}", user);
            if (StringUtils.isEmpty(user.getUsername())) {
                LOGGER.error(ErrorConstant.USERNAME_IS_NULL);
                response.setStatus(500);
                return Result.error(ErrorConstant.USERNAME_IS_NULL);
            } else if (StringUtils.isEmpty(user.getPassword())) {
                LOGGER.error(ErrorConstant.PASSWORD_IS_NULL);
                response.setStatus(500);
                return Result.error(ErrorConstant.PASSWORD_IS_NULL);}
//            } else if (StringUtils.isEmpty(user.getConfirmPassword())) {
//                LOGGER.error(ErrorConstant.CONFIRM_PASSWORD_IS_NULL);
//                response.setStatus(500);
//                return Result.error(ErrorConstant.CONFIRM_PASSWORD_IS_NULL);
//            } else if (user.getConfirmPassword() != user.getPassword()) {
//                LOGGER.error(ErrorConstant.DIFFERENT_PASSWORD);
//                response.setStatus(500);
//                return Result.error(ErrorConstant.DIFFERENT_PASSWORD);
//            }
            int count = userService.registerUser(user);
            if (count != 1) {
                LOGGER.error(ErrorConstant.PASSWORD_ERROR);
                response.setStatus(500);
                return Result.error(ErrorConstant.PASSWORD_ERROR);
            }
            return result.ok();
        } catch (Exception e) {
            LOGGER.error("用户注册失败", e);
            response.setStatus(500);
            return result.error("注册失败");
        }
    }
}
